package com.campus.happy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.campus.utils.StreamUtils;

public class CookieHttp {
	/*
	 * post���� url url p ���� encode ���뷽ʽ
	 */
	public static String post(String url, String p, String encode)
			throws MalformedURLException, IOException {

		URLConnection conn = new URL(url).openConnection();
		String s;
		conn.setRequestProperty("Origin", "http://218.195.32.11");
		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		// conn.setRequestProperty(
		// "Referer",
		// "http://218.195.32.11/(icq1h545n1xvg545qtz4fwro)/xxjsjy.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121611");
		conn.setDoInput(true);
		conn.setDoOutput(true);

		if (p != null)
			conn.getOutputStream().write(p.getBytes());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), encode));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}

	public static String get(String url, String encode)
			throws MalformedURLException, IOException {

		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty(
				"Cookie",
				"navCtgScroll=100; showNav=#nav-tab|0|1; PHOENIX_ID=0a01678a-1549eec6c38-a8c2fe; _hc.v=\"\\\"85363e3a-9883-4542-885a-4aa7021c0f45.1462955174\"\"; _tr.u=Cg6rE0QvsCbyNa8i; _tr.s=2VLYWH290Z6ku1Zr; s_ViewType=10; JSESSIONID=186D6024CBA5E008CAE6CD47DF5AC624; aburl=1; cy=17; cye=xian");
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		// conn.setRequestProperty("Referer",
		// "http://218.195.32.11/(yh0kxoaa40kpjamm4u0myp55)/xs_main.aspx?xh=1205500121");
		String s;
		System.out.println(conn.getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), encode));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}

	public static byte[] get(String url) throws MalformedURLException,
			IOException {

		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty(
				"Cookie",
				"navCtgScroll=100; showNav=#nav-tab|0|1; PHOENIX_ID=0a01678a-1549eec6c38-a8c2fe; _hc.v=\"\\\"85363e3a-9883-4542-885a-4aa7021c0f45.1462955174\"\"; _tr.u=Cg6rE0QvsCbyNa8i; _tr.s=2VLYWH290Z6ku1Zr; s_ViewType=10; JSESSIONID=186D6024CBA5E008CAE6CD47DF5AC624; aburl=1; cy=17; cye=xian");
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		// conn.setRequestProperty("Referer",
		// "http://218.195.32.11/(yh0kxoaa40kpjamm4u0myp55)/xs_main.aspx?xh=1205500121");
		String s;
		System.out.println(conn.getResponseCode());

		return StreamUtils.Stream2byte(conn.getInputStream());

	}

}
